package cn.new7as.cas5s.ui.apply;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.easy.netapi.PointApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityInfoForthBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.GetBaseDataApi;
import cn.new7as.cas5s.ui.easy.netapi.SaveBaseDataApi;


public class Info_ForthActivity extends BaseActivity {


    private ActivityInfoForthBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInfoForthBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Step4", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(Info_ForthActivity.this, Info_ThirdActivity.class);
                finish();
            }
        });
        show();
        getBase();
        initV();

    }

    @Override
    public void initV() {
        super.initV();
        setPoint(binding.accountnumberBa, 1);
        setPoint(binding.accountnumberconfirmationBa, 2);
        setPoint(binding.ifscBa, 3);
        binding.ifscBa.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        setEtListener(binding.ifscBa, binding.accountnumberBa, binding.accountnumberconfirmationBa);

        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ifscBa = binding.ifscBa.getText().toString();
                String accountnumberBa = binding.accountnumberBa.getText().toString();
                String accountnumberconfirmationBa = binding.accountnumberconfirmationBa.getText().toString();
                String upiBa = binding.upiBa.getText().toString();

                SaveBaseDataApi.Data data = new SaveBaseDataApi().new Data();
                data.type = 2;
                SaveBaseDataApi.Data4 data1 = new SaveBaseDataApi().new Data4();
                data1.code = ifscBa;
                data1.number = accountnumberBa;
                data1.confirmNumber = accountnumberconfirmationBa;
                data1.upi = upiBa;
                data.bankDetailInfo = data1;
                show();
                EasyHttp.post(Info_ForthActivity.this)
                        .api(new SaveBaseDataApi(getunique(), null, 2, null, null, data1)).request(new OnHttpListener<HttpData<Object>>() {
                            @Override
                            public void onHttpSuccess(@NonNull HttpData<Object> result) {
                                point(getunique());
                            }

                            @Override
                            public void onHttpFail(@NonNull Throwable throwable) {
                                deal(throwable);

                            }
                        });
            }


        });
    }

    private int ba;

    @Override
    protected void onStop() {
        super.onStop();
        ba++;
    }


    long editStart;
    int accountcount;
    int accountDuration;
    int againcount;
    int againDuration;
    int ifsccount;
    int ifscDuration;

    private void point(String s) {
        long end = System.currentTimeMillis();
        PointApi.Point5Data pointData = new PointApi().new Point5Data();
        pointData.type = 2;

        pointData.basicpage_backend_num = ba;


        pointData.bankno_updatecount = accountcount;
        pointData.bankno_duration = accountDuration;

        pointData.bankno_confirmation_updatecount = againcount;
        pointData.bankno_confirmation_duration = againDuration;

        pointData.ifscode_updatecount = ifsccount;
        pointData.ifscode_duration = ifscDuration;


        pointData.bankpage_stop_duration = (int) ((end - start) / 1000);
        pointData.frist_click_time = Utils.getdate(start);
        pointData.end_click_time = Utils.getdate(end);
        EasyHttp.post(this)
                .api(new PointApi(s, pointData)).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {
                        hide();
                        finish();

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });

    }

    void setPoint(EditText editText, int type) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    switch (type) {
                        case 1:
                            accountcount++;
                            break;
                        case 2:
                            againcount++;
                            break;
                        case 3:
                            ifsccount++;
                            break;
                        case 4:
                            break;
                    }

                    editStart = System.currentTimeMillis();
                } else {
                    int i = (int) ((System.currentTimeMillis() - editStart) / 1000);
                    switch (type) {
                        case 1:
                            accountDuration += i;
                            break;
                        case 2:
                            againDuration += i;
                            break;
                        case 3:
                            ifscDuration++;
                            break;
                        case 4:

                            break;
                    }

                }
            }
        });
    }

    void getBase() {
        EasyHttp.post(Info_ForthActivity.this)
                .api(new GetBaseDataApi(getunique(), 2)).request(new OnHttpListener<HttpData<GetBaseDataApi.Data>>() {


                    @Override
                    public void onHttpSuccess(@NonNull HttpData<GetBaseDataApi.Data> result) {
                        hide();
                        if (result.getResult() != null) {
                            binding.ifscBa.setText(result.getResult().code);
                            binding.accountnumberBa.setText(result.getResult().number);
                            binding.accountnumberconfirmationBa.setText(result.getResult().number);
                            binding.upiBa.setText(result.getResult().upi);

                            setTrue();
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

    @Override
    public void setFalse() {
        super.setFalse();
        binding.verifyme.setEnabled(false);
    }

    @Override
    public void setTrue() {
        super.setTrue();
        if (Utils.isStr(binding.ifscBa, binding.accountnumberBa, binding.accountnumberconfirmationBa))
            binding.verifyme.setEnabled(true);
    }


}